//
//  EnrollLoginController.m
//  lv678
//
//  Created by apple on 15/8/31.
//  Copyright (c) 2015年 lampshade. All rights reserved.
//

#import "EnrollLoginController.h"
#import "AFNetworking.h"
#import "TravelEnorllController.h"
#import "Reachability.h"
#import "CommonLoading.h"

@interface EnrollLoginController ()
@property (weak, nonatomic) IBOutlet UIButton *backBtn;//返回按钮
@property (weak, nonatomic) IBOutlet UITextField *memberCard;//会员卡号
@property (weak, nonatomic) IBOutlet UITextField *password;//密码
- (IBAction)loginBtn:(id)sender;

@end

@implementation EnrollLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}
#pragma mark - 初始化视图
- (void)initView{
    self.memberCard.delegate = self;
    self.password.delegate = self;
    [self.backBtn addTarget:self action:@selector(backBtnLast) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 关闭软键盘
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#pragma mark - 点击软键盘return关闭软键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}

#pragma mark - 返回根视图
- (void)backBtnLast{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - 验证登陆信息
- (IBAction)loginBtn:(id)sender {
    UIView *login = [[CommonLoading initCommonLoading]loginLoading];
    [self.view addSubview:login];
    TravelEnorllController *travelEnorll = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"travelEnroll"];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"UrlList.plist" ofType:nil];
    NSDictionary *tempDict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSString *url = [tempDict objectForKey:@"tripapplyapp"];
    NSDictionary *data = @{@"serviceName":@"login",@"queryParameters":
                               @{@"cardNo":self.memberCard.text,@"cardpwd":self.password.text}};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    [manager POST:url parameters:data success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dataList = [responseObject objectForKey:@"dataList"];
                int status = [[responseObject objectForKey:@"status"]intValue];
                NSString *comment = [responseObject objectForKey:@"comment"];
                [login removeFromSuperview];
                if (status == 1) {
                    
                    NSString *applyId = [dataList valueForKey:@"applyId"];
                    if ([self.delegate respondsToSelector:@selector(sendValue:)]) {
                        [self.delegate sendValue:applyId];
                    }
                    [self.navigationController pushViewController:travelEnorll animated:YES];
        
                    }else{
                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:comment delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                        [alert show];
                }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [login removeFromSuperview];
                NSLog(@"登录失败%@",error);

    }];
}

@end
